

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  <meta name="description" content="今天说一个非常有趣的css属性content，可能很多人都会说，这个属性谁会不知道，对于一些老手来说，这个属性确实可能是经常会用到，但是总会有你没有发现的地方，今天就来好好的讲讲这个有趣的css属性。以下知识内容均来自于张鑫旭的《css世界》，有兴趣的可以去看看。 content 介绍content用于元素内容的展示，平常用的多的地方在于内容前后的具体文字内容或者是图片。图片其实没有很多好介绍，但">
<meta property="og:type" content="article">
<meta property="og:title" content="有趣的css属性content">
<meta property="og:url" content="http://yoursite.com/2020/08/13/%E6%9C%89%E8%B6%A3%E7%9A%84css%E5%B1%9E%E6%80%A7content/index.html">
<meta property="og:site_name" content="Food-Billboard">
<meta property="og:description" content="今天说一个非常有趣的css属性content，可能很多人都会说，这个属性谁会不知道，对于一些老手来说，这个属性确实可能是经常会用到，但是总会有你没有发现的地方，今天就来好好的讲讲这个有趣的css属性。以下知识内容均来自于张鑫旭的《css世界》，有兴趣的可以去看看。 content 介绍content用于元素内容的展示，平常用的多的地方在于内容前后的具体文字内容或者是图片。图片其实没有很多好介绍，但">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/url-show.png">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/attr-show.png">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/loading.GIF">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/quotes-show.png">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/counter-init-show.png">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/counter-increment-show.png">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/quotes-show.png">
<meta property="og:image" content="http://yoursite.com/images/有趣的css属性content/counters.png">
<meta property="article:published_time" content="2020-08-13T13:57:00.000Z">
<meta property="article:modified_time" content="2021-10-26T11:38:18.000Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="CSS">
<meta property="article:tag" content="content">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://yoursite.com/images/有趣的css属性content/url-show.png">
  
  <title>有趣的css属性content - Food-Billboard</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<!-- hexo injector head_end start -->
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css"></link>
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css"></link>
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/project">
                <i class="iconfont icon-books"></i>
                项目
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/%E6%9C%89%E8%B6%A3%E7%9A%84css%E5%B1%9E%E6%80%A7content/quotes-show.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="有趣的css属性content">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-08-13 21:57" pubdate>
        2020年8月13日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.7k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      15 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">有趣的css属性content</h1>
            
            <div class="markdown-body">
              <p>今天说一个非常有趣的<code>css</code>属性<code>content</code>，可能很多人都会说，这个属性谁会不知道，对于一些老手来说，这个属性确实可能是经常会用到，但是总会有你没有发现的地方，今天就来好好的讲讲这个有趣的<code>css</code>属性。<br>以下知识内容均来自于<a target="_blank" rel="noopener" href="https://www.zhangxinxu.com/">张鑫旭的《css世界》</a>，有兴趣的可以去看看。</p>
<h2 id="content-介绍"><a href="#content-介绍" class="headerlink" title="content 介绍"></a>content 介绍</h2><p><code>content</code>用于元素内容的展示，平常用的多的地方在于内容前后的具体文字内容或者是图片。图片其实没有很多好介绍，但是关于文字方面，它有很多种的类型，下面就一一将<code>content</code>的值类型进行介绍。</p>
<h3 id="content-属性"><a href="#content-属性" class="headerlink" title="content 属性"></a>content 属性</h3><ol>
<li><strong>normal</strong>  </li>
</ol>
<p>这是<code>content</code>属性的默认值，在伪元素中会被认定为<code>none</code></p>
<ol start="2">
<li><strong>none</strong> </li>
</ol>
<p>指定不产生伪元素</p>
<ol start="3">
<li><strong>url</strong>  </li>
</ol>
<p>可以使用图片来进行展示，比如</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css">  <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">text-align</span>: center;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">::after</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">&#x27;/images/有趣的css属性content/css世界.jpg&#x27;</span>);</span><br><span class="css">    <span class="hljs-attribute">display</span>: block;</span><br><span class="css">  &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>效果如下图</p>
<img src="/images/有趣的css属性content/url-show.png" srcset="/img/loading.gif" lazyload width="188" height="334" />

<p><code>url</code>的参数有很多种，最常用的就是路径<br>然后就是<code>icon</code>、<code>svg</code>、<code>base64</code>编码的图片，但是不支持<code>css3渐变</code>背景图。  </p>
<p>虽然有这种方法可以生成图片，但是实际中并无太大用处，原因就在于无法通过设置宽高来改变图片的固有尺寸。  </p>
<p>一般都是使用<code>background-image</code>来实现相关的功能。</p>
<p>不过<code>base64</code>形式却能解决上面的问题<br>一般情况下图片在加载过程中都会存在尺寸为<code>0</code>的时候，所以在加载完成时页面会存在图片晃动的情况。<br><code>base64</code>图片没有这个过程。<br>但是<code>base64</code>也有不好的地方，比如，<code>base64</code>编码后的图片体积比之前要大上1/3左右。</p>
<ol start="4">
<li><strong>attr</strong>  </li>
</ol>
<p><code>attr</code>和它名字一样，它能够选择到该元素身上的属性。<br>比如</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css"><span class="hljs-selector-tag">img</span>&#123;</span><br><span class="css">  <span class="hljs-attribute">text-align</span>:center;</span><br><span class="css">&#125;</span><br><span class="css"><span class="hljs-selector-tag">img</span><span class="hljs-selector-pseudo">::after</span>&#123;</span><br><span class="css">  <span class="hljs-attribute">content</span>: <span class="hljs-built_in">attr</span>(width);  </span><br><span class="css">&#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;&quot;</span> <span class="hljs-attr">width</span>=<span class="hljs-string">&quot;100&quot;</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">&quot;图片&quot;</span> /&gt;</span><br></code></pre></td></tr></table></figure>
<img src="/images/有趣的css属性content/attr-show.png" srcset="/img/loading.gif" lazyload width="188" height="334" />

<p>这样可以显示到<code>img</code>元素的<code>width</code>属性的值。<br>并且，它还能够获取到自定义属性的值，比如<code>attr(data-name)</code></p>
<p>注意一下，这里的属性名称是不需要<strong>引号</strong>的。</p>
<ol start="5">
<li><strong>string</strong>  </li>
</ol>
<p>这个的话我想应该不用多说，大家用的也是相当熟练的。但是不知道大家有没有使用过<code>Unicode</code>字符？<br>在书中发现了一个利用此特性的小栗子🌰，感觉非常好用，这里写出来给大家参考一下。<br>下面实现了一个动态loading加载的效果。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css">  dot&#123;</span><br><span class="css">    <span class="hljs-attribute">display</span>:inline-block;</span><br><span class="css">    <span class="hljs-attribute">height</span>:<span class="hljs-number">1em</span>;</span><br><span class="css">    <span class="hljs-attribute">line-height</span>:<span class="hljs-number">1</span>;</span><br><span class="css">    <span class="hljs-attribute">overflow</span>:hidden;</span><br><span class="css">    <span class="hljs-attribute">text-align</span><span class="hljs-selector-pseudo">:left</span>;</span><br><span class="css">    <span class="hljs-attribute">vertical-align</span>: -.<span class="hljs-number">25em</span>;</span><br><span class="css">  &#125;</span><br><span class="css">  dot<span class="hljs-selector-pseudo">::before</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: <span class="hljs-string">&#x27;...\A..\A.&#x27;</span>;</span><br><span class="css">    <span class="hljs-attribute">animation</span>: loading <span class="hljs-number">3s</span> infinite step-start both;</span><br><span class="css">    <span class="hljs-attribute">display</span>:block;</span><br><span class="css">    <span class="hljs-attribute">white-space</span>: pre-wrap;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-keyword">@keyframes</span> loading &#123;</span><br><span class="css">    <span class="hljs-number">33%</span> &#123; <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">2em</span>); &#125;</span><br><span class="css">    <span class="hljs-number">66%</span> &#123; <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">1em</span>); &#125;</span><br><span class="css">  &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>加载中<br>  <span class="hljs-tag">&lt;<span class="hljs-name">dot</span>&gt;</span>...<span class="hljs-tag">&lt;/<span class="hljs-name">dot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>下面是效果，可能图片看起来有些不流畅，但是实际效果是流畅的。   </p>
<img src="/images/有趣的css属性content/loading.GIF" srcset="/img/loading.gif" lazyload width="376" height="668" />

<p>这一方法的好处就是相当好的兼容性，它利用上述特性和<code>css3</code>的动画效果来实现。 </p>
<p>并且在低版本浏览器下虽然支持不了<code>animation</code>属性，但是它也能良好的展示静态效果。</p>
<ol start="6">
<li><strong>open-quote、close-quote</strong>  </li>
</ol>
<p>关于这个属性，算是一个好用却又不实用的属性吧，它本身良好的特性可以完成平常的许多业务问题，但是却又显得没有那么必要。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css">  <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">:nth-of-type</span>(<span class="hljs-number">1</span>)&#123;</span><br><span class="css">    <span class="hljs-attribute">quotes</span>: <span class="hljs-string">&#x27;提问: &quot;&#x27;</span> <span class="hljs-string">&#x27;&quot;&#x27;</span>;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">:nth-of-type</span>(<span class="hljs-number">1</span>)&#123;</span><br><span class="css">    <span class="hljs-attribute">quotes</span>: <span class="hljs-string">&#x27;回答: &quot;&#x27;</span> <span class="hljs-string">&#x27;&quot;&#x27;</span>;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">::before</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: open-quote;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">::after</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: close-quote;</span><br><span class="css">  &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;ques&quot;</span>&gt;</span>为什么我这么帅？<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;answ&quot;</span>&gt;</span>天生的！<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>
<img src="/images/有趣的css属性content/quotes-show.png" srcset="/img/loading.gif" lazyload width="188" height="334" />

<p>还有<code>no-open-quote</code> <code>no-close-quote</code>，和名字一下，就是在开始和结束不需要该内容。</p>
<ol start="7">
<li><strong>counter</strong>  </li>
</ol>
<p>接下来最后要着重介绍的一个属性，<code>counter</code>。计数器。  </p>
<p>大家应该有见过<code>word</code>文档中的那种标题吧，其实那种样式可以通过<code>css</code>来自动生成。<br>没有错，用到的就是现在介绍的这个属性。<br>它只需要几行css代码就能实现那样的效果。  </p>
<p>首先是相关的几个属性。<br><code>counter-reset</code> <code>counter-increment</code> <code>counter()</code> <code>counters()</code>  </p>
<p>使用步骤如下：</p>
<ul>
<li><strong>定义计数器名称 counter-reset</strong>  </li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css">  <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">counter-reset</span>: counter-name <span class="hljs-number">2</span>;  </span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">::after</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: <span class="hljs-built_in">counter</span>(counter-name);</span><br><span class="css">  &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>这是counter计数器的初始值: <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>上面定义了一个计数器，名称叫<code>counter-name</code> 并且起始为2<br><img src="/images/有趣的css属性content/counter-init-show.png" srcset="/img/loading.gif" lazyload width="188" height="334" /></p>
<p>关于后面<code>起始数字</code>的问题，也可以是<code>负数</code>，也可以写成<code>小数</code>。<br>不过对于<code>ie</code>和<code>firefox</code>是直接无视的，默认为<code>0</code>，而对于<code>chrome</code>来说则是处理成向下取整。  </p>
<p>你也可以同时定义多个计数器。如下<br><code>counter-reset: coutner-name1 3 counter-name2 4</code>  </p>
<ul>
<li><strong>递增</strong>  </li>
</ul>
<p>第一步中实现的是显示计数器的值，而此步骤则是真正能让计数器开始计数的关键。  </p>
<p><code>counter-increment</code> 用来让计数器累加，如下</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css">  <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">counter-reset</span>: counter;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">::before</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: <span class="hljs-string">&#x27;递增前我是: &#x27;</span> <span class="hljs-built_in">counter</span>(counter);</span><br><span class="css">    <span class="hljs-attribute">display</span>:block;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">::after</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">counter-increment</span>: counter <span class="hljs-number">2</span>;</span><br><span class="css">    <span class="hljs-attribute">content</span>: <span class="hljs-string">&#x27;递增后我是: &#x27;</span> <span class="hljs-built_in">counter</span>(counter);</span><br><span class="css">  &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>如下图所示  </p>
<img src="/images/有趣的css属性content/counter-increment-show.png" srcset="/img/loading.gif" lazyload width="188" height="334" />  

<p>上面指定了<code>counter</code>计数器增加了<code>2</code>，后面的数字也可以是<code>负值</code>，这样就实现了递减的效果。  </p>
<p>需要注意的是，虽然在不同元素上计数器并不会产生效果，但是如果在其中使用了<code>counter-increment</code>那么它就会产生效果。  </p>
<p><code>counter()</code>和<code>counters()</code>还有一些参数可以选择  </p>
<blockquote>
<p>counter(name, style)<br>counters(name, string)<br>counters(name, string, style)  </p>
</blockquote>
<p><code>strig</code>就是连接符  </p>
<p><code>style</code>的话，它的值和<code>list-style</code>的值相同<br>所以在展示时，可以不一定显示数字，也可以显示其他的字符。<br>具体可以参考 <a target="_blank" rel="noopener" href="https://www.w3school.com.cn/css/pr_list-style-type.asp">list-style</a>  </p>
<p>比如下面  </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css"></span><br><span class="css">  <span class="hljs-selector-class">.box</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">counter-reset</span>: counter;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-class">.box</span><span class="hljs-selector-pseudo">::before</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">content</span>: <span class="hljs-string">&#x27;我有多帅?&#x27;</span>;</span><br><span class="css">  &#125;</span><br><span class="css">  <span class="hljs-selector-class">.counter</span><span class="hljs-selector-pseudo">::after</span>&#123;</span><br><span class="css">    <span class="hljs-attribute">counter-increment</span>: counter;</span><br><span class="css">    <span class="hljs-attribute">quotes</span>: <span class="hljs-string">&#x27;绝对&quot;&#x27;</span> <span class="hljs-string">&#x27;+&quot;啊&#x27;</span>;</span><br><span class="css">    <span class="hljs-attribute">content</span>: open-quote <span class="hljs-built_in">counters</span>(counter, <span class="hljs-string">&#x27;-&#x27;</span>, upper-latin) close-quote;</span><br><span class="css">  &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;box&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;counter&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>  <br></code></pre></td></tr></table></figure>

<p>效果如下图<br><img src="/images/有趣的css属性content/quotes-show.png" srcset="/img/loading.gif" lazyload width="188" height="334" />  </p>
<p>关于前面说的<code>word</code>文档标题的级联效果，用到的就是<code>counters()</code>这个方法  </p>
<p>下面是简单的效果。  </p>
<pre><code class="html"><span class="tag">&lt;<span class="name">style</span>&gt;</span>
  .reset&#123;
    counter-reset: counter;
  &#125;
  .counter::after&#123;
    counter-increament: counter;
    content: couters(counter, '-') '.';
  &#125;
<span class="tag">&lt;/<span class="name">style</span>&gt;</span>
<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"reset"</span>&gt;</span>
  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>
    第一
    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"reset"</span>&gt;</span>
      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>第一一<span class="tag">&lt;/<span class="name">div</span>&gt;</span>
      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>第一二<span class="tag">&lt;/<span class="name">div</span>&gt;</span>
    <span class="tag">&lt;/<span class="name">div</span>&gt;</span>
  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>
  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>
    第二
    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"reset"</span>&gt;</span>
      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>
        第二一
        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"reset"</span>&gt;</span>
          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>第二一<span class="tag">&lt;/<span class="name">div</span>&gt;</span>
          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>第二二<span class="tag">&lt;/<span class="name">div</span>&gt;</span>
        <span class="tag">&lt;/<span class="name">div</span>&gt;</span>
      <span class="tag">&lt;/<span class="name">div</span>&gt;</span>
      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>第二二<span class="tag">&lt;/<span class="name">div</span>&gt;</span>
    <span class="tag">&lt;/<span class="name">div</span>&gt;</span>
  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  
  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"counter"</span>&gt;</span>
    第三
  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>
<span class="tag">&lt;/<span class="name">div</span>&gt;</span></code></pre>
<p>效果如下图  </p>
<img src="/images/有趣的css属性content/counters.png" srcset="/img/loading.gif" lazyload width="188" height="334" />  

<h2 id="完结"><a href="#完结" class="headerlink" title="完结"></a>完结</h2><p>合理的使用css属性可以让你在实际开发过程中减少很多的工作，使用css完成的效果总比用css完成性能要好的多。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/CSS/">CSS</a>
                    
                      <a class="hover-with-bg" href="/categories/CSS/%E5%89%8D%E7%AB%AF/">前端</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/CSS/">CSS</a>
                    
                      <a class="hover-with-bg" href="/tags/content/">content</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/08/28/Chai/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Chai</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/07/26/React%E6%97%A5%E5%8E%86%E7%BB%84%E4%BB%B6/">
                        <span class="hidden-mobile">React日历组件</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span> <script src="/js/duration.js"></script> <script src="/js/code-viewer.js"></script> </div> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


<!-- hexo injector body_end start -->
  <script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiantiao.js"></script>
  <script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxingxing.js"></script>
  <script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/dianjichuzi.js"></script>
<!-- hexo injector body_end end --><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"right","width":150,"height":300,"mobile":{"show":true}},"log":false});</script></body>
</html>
